package rpg.data;

import java.util.List;

import org.jdom.Element;

import pedrosm.data.ScriptData;

import square.data.ConditionData;
import square.data.DataGroup;
import square.script.ConditionScript;

/**
 * 
 * @author Pedro Silva Moreira
 */
public class ConditionXML implements LoaderXML {

	@SuppressWarnings("unchecked")
	public void load(String group, String path, Element origin) {
		Element elem = XML.loadXML(path);

		ConditionData data = new ConditionData();
		data.setGroup(group);
		data.setName(elem.getAttributeValue("name"));
		
		String script = elem.getAttributeValue("script");
		if(script != null) {
			data.setScript((ScriptData<ConditionScript>) DataGroup.findScript(script));			
		}

		// Attributes
		List<Element> attributes = elem.getChildren("attr");
		for (Element it : attributes) {
			String name = it.getAttributeValue("name");
			String value = it.getAttributeValue("value");

			if (name.equals("time")) {
				data.setTime(Integer.valueOf(value));
			}
		}

		// Type
		List<Element> types = elem.getChildren("type");
		for (Element it : types) {
			data.getTypes().add(it.getAttributeValue("name"));
		}

		// Add to Data
		DataGroup.getConditions().add(data);
	}
}
